/* Data analysis of Endline Data for Nepal CCF: Women */

local folder "C:\Users\mlevere\OneDrive - Mathematica\Documents\Projects\Nepal\"

/* Merge together all the sections into one big dataset 
   Rename all the variables along the way */
use "`folder'/Data/Midline_Raw/SEC01", clear
rename q01_101 round_num
rename q01_102 co_name
rename q01_103 co_code
rename q01_104 house_sn
rename q01_104sn house_sn_sub
rename q01_105a num_elig_women
rename q01_105b int_elig_women
rename q01_106 co_mem_name
rename q01_106b baseline_hh
rename q01_107dd interview_day
rename q01_107mm interview_month
rename q01_107yy interview_year
rename q01_108 interviewer_name
rename q01_109 supervisor_name
rename q01_110 editor_name
rename q01_111 dataentry_name
tempfile midline_raw_women
save "`midline_raw_women'", replace

merge 1:1 www hh sn using "`folder'/Data/Midline_Raw/SEC02", nogen
rename q02_201 district
rename q02_202 vdc
rename q02_203a vdc_name
rename q02_203b vdc_code
rename q02_204 ward_num
rename q02_205 village_name
rename q02_206a1 main_respondent_name
rename q02_206a2 main_respondent_id
rename q02_206b1 second_respondent_name
rename q02_206b2 second_respondent_id
rename q02_207a hh_head_name
rename q02_207b hh_head_id
rename q02_208 hh_head_caste
rename q02_209 spouse_name
rename q02_210 houseleft_name
rename q02_211 houseright_name
rename q02_212 survey_result
rename q02_213 religion
rename q02_214 tot_family
save "`midline_raw_women'", replace

/* Get ID Code of the eligible woman respondent (if missing, likely means survey not completed)
   Merge this in to get her demographics from the HH roster */
merge 1:m www hh sn using "`folder'/Data/Midline_Raw/SEC06A", nogen keepusing(idc)
sort www hh sn
order www hh sn idc
drop if idc == .
save "`midline_raw_women'", replace

/* Rename the ID Code variable in order to merge with Section 03 */
rename idc q03_idc
merge 1:1 www hh sn q03_idc using "`folder'/Data/Midline_Raw/SEC03", nogen keep(matches)
rename q03_301 name
rename q03_302 relationship
rename q03_303 sex
rename q03_304a age_years
rename q03_304b age_months
rename q03_305 literacy
rename q03_306 school
rename q03_307 highest_grade
rename q03_308 marital_status
rename q03_309a occupation_main
rename q03_309b occupation_secondary
rename q03_310 residing
rename q03_311 eligible
rename q03_idc idc
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC06A", nogen
rename q06_601 sick
rename q06_chk01 num_sick
save "`midline_raw_women'", replace

/* Merge in the actual sickness stuff, only want for the woman herself */
keep www hh sn idc
merge 1:m www hh sn idc using "`folder'/Data/Midline_Raw/SEC06B", nogen keep(3)
keep if idc == q06_idc
tempfile sec06B_sickwomen
save "`sec06B_sickwomen'", replace

use "`midline_raw_women'", clear
merge 1:m www hh sn idc using "`sec06B_sickwomen'", nogen keep(1 3)
drop q06_602 q06_idc
rename q06_603 illness_type
rename q06_604 illness_where
rename q06_605 illness_consult_whom
rename q06_606 illness_noconsult
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC07A", nogen
rename q07_701 knowledge_firstmilk
rename q07_702 knowledge_exclusivebreastfeed
save "`midline_raw_women'", replace

use "`folder'/Data/Midline_Raw/SEC07B", clear
reshape wide q07_703b, i(www hh sn idc) j(q07_703a)
local fooditems "porridge_ue porridge_e vegetables fruits eggs milk meat oil lentils biscuit cerelac horlicks noodles vitamin bread others"
local i = 1
local n : word count `fooditems'
while `i' < = `n' {
local change : word `i' of `fooditems'
rename q07_703b`i' goodfoods_kid_`change'
label variable goodfoods_kid_`change' "703. What foods are best for kid? `change' mentioned"
local i = `i' + 1
}
tempfile SEC07B_wide
save "`SEC07B_wide'", replace

use "`midline_raw_women'", clear
merge 1:1 www hh sn idc using "`SEC07B_wide'", nogen
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC07C", nogen
rename q07_704 knowledge_6t11_fed
rename q07_705 knowledge_12t24_snacks
rename q07_706 knowledge_breastfeed_diahrrea
rename q07_707 knowledge_anc_checkups
rename q07_708 knowledge_iron_supp
rename q07_709 knowledge_deworming
rename q07_710 knowledge_vitaminA
rename q07_711 knowledge_pregnant_food
save "`midline_raw_women'", replace

use "`folder'/Data/Midline_Raw/SEC07D", clear
reshape wide q07_712b, i(www hh sn idc) j(q07_712a)
local fooditems "vegetables fruits eggs milk meat beans rice bread horlicks vitamin noodles biscuit haluwa other"
local i = 1
local n : word count `fooditems'
while `i' < = `n' {
local change : word `i' of `fooditems'
rename q07_712b`i' goodfoods_preg_`change'
label variable goodfoods_preg_`change' "712. What foods are best for pregnant woman? `change' mentioned"
local i = `i' + 1
}
tempfile SEC07D_wide
save "`SEC07D_wide'", replace

use "`midline_raw_women'", clear
merge 1:1 www hh sn idc using "`SEC07D_wide'", nogen
save "`midline_raw_women'", replace

use "`folder'/Data/Midline_Raw/SEC07E", clear
reshape wide q07_713b, i(www hh sn idc) j(q07_713a)
local advice "fchv aama mchw othhealth pharmacy tradhealers family other"
local i = 1
local n : word count `advice'
while `i' < = `n' {
local change : word `i' of `advice'
rename q07_713b`i' seekadvice_`change'
label variable seekadvice_`change' "713. Do you usually seek advice with `change'?"
local i = `i' + 1
}
tempfile SEC07E_wide
save "`SEC07E_wide'", replace

use "`midline_raw_women'", clear
merge 1:1 www hh sn idc using "`SEC07E_wide'", nogen
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC07F", nogen
rename q07_714 salt_type
rename q07_715 tobacco_alcohol_use
rename q07_716a health_ranking
rename q07_716b food_ranking
rename q07_716c education_ranking
rename q07_716d security_ranking
rename q07_717 child_educ_expect
rename q07_718 attend_co_meeting
rename q07_719 child_occ_expect
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC08A", nogen
rename q08_801 age_marriage
rename q08_802 marriage_arranged
rename q08_803 curr_pregnant
rename q08_804 length_pregnant
rename q08_805 first_pregnancy
rename q08_806 youngest_antenatal_care
rename q08_807 youngest_tt_injection
rename q08_808 youngest_tt_injection_num
rename q08_809 youngest_irontab
rename q08_810 youngest_irontab_num
save "`midline_raw_women'", replace

use "`folder'/Data/Midline_Raw/SEC08B", clear
reshape wide q08_811b, i(www hh sn idc) j(q08_811a)
local places "fchv facility clinic hospital ngo nursinghome other"
local i = 1
local n : word count `places'
while `i' < = `n' {
local change : word `i' of `places'
rename q08_811b`i' youngest_irontab_`change'
label variable youngest_irontab_`change' "811. Iron tablets were taken from `change'"
local i = `i' + 1
}
tempfile SEC08B_wide
save "`SEC08B_wide'", replace

use "`midline_raw_women'", clear
merge 1:1 www hh sn idc using "`SEC08B_wide'", nogen
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC08C", nogen
rename q08_812 youngest_deworming
rename q08_813 youngest_nightblindness
rename q08_814 youngest_birthplace
rename q08_815 youngest_deliveryhelp
rename q08_816 youngest_vitaminA
rename q08_817 youngest_vitaminA_place
rename q08_818 family_planning
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC09A", nogen
rename q09_901 youngest_breastfed
rename q09_902 youngest_breastfed_first
rename q09_903 youngest_breastfed_3days
rename q09_904 youngest_3days_nonbreastmilk
save "`midline_raw_women'", replace

use "`folder'/Data/Midline_Raw/SEC09B", clear
reshape wide q09_905b, i(www hh sn idc) j(q09_905a)
local othfoods "milk_nonbreast oth_mother_milk water sugar_water gripe_water ssw_solution fruit_juice formula honey ghee milk_powder other"
local i = 1
local n : word count `othfoods'
while `i' < = `n' {
local change : word `i' of `othfoods'
rename q09_905b`i' youngest_fed_`change'
label variable youngest_fed_`change' "905. (Name) fed `change'"
local i = `i' + 1
}
tempfile SEC09B_wide
save "`SEC09B_wide'", replace

use "`midline_raw_women'", clear
merge 1:1 www hh sn idc using "`SEC09B_wide'", nogen
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC09C", nogen
rename q09_906 youngest_breastfed_exclusive
rename q09_907 youngest_breastfed_length
rename q09_908 youngest_over24months
rename q09_909 youngest_breastfeed_current
rename q09_910 youngest_breastfed_past24hrs
save "`midline_raw_women'", replace

use "`folder'/Data/Midline_Raw/SEC09D", clear
reshape wide q09_911b, i(www hh sn idc) j(q09_911a)
local reasons "nomilk inadequate workload childill pregnant mothersick other"
local i = 1
local n : word count `reasons'
while `i' < = `n' {
local change : word `i' of `reasons'
rename q09_911b`i' youngest_nofed_`change'
label variable youngest_nofed_`change' "911. Reasons not breastfed `change'"
local i = `i' + 1
}
tempfile SEC09D_wide
save "`SEC09D_wide'", replace

use "`midline_raw_women'", clear
merge 1:1 www hh sn idc using "`SEC09D_wide'", nogen
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC09E", nogen
rename q09_912 youngest_bottle_past24hrs
save "`midline_raw_women'", replace

use "`folder'/Data/Midline_Raw/SEC09F", clear
reshape wide q09_913b, i(www hh sn idc) j(q09_913a)
local foods "breastmilk water formula fortified porridge animalmilk yogurt rice_grains potatoes yo_veg yo_fruits leafy_veg oth_fruit eggs meat organ_meat fish snails lentils oils tea sugary biscuits other"
local i = 1
local n : word count `foods'
while `i' < = `n' {
local change : word `i' of `foods'
rename q09_913b`i' youngest_past24hrs_`change'
label variable youngest_past24hrs_`change' "913. Fed `change' in past 24 hours"
local i = `i' + 1
}
tempfile SEC09F_wide
save "`SEC09F_wide'", replace

use "`midline_raw_women'", clear
merge 1:1 www hh sn idc using "`SEC09F_wide'", nogen
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC09G", nogen
rename q09_914 youngest_past24hrs_nummeals
save "`midline_raw_women'", replace

use "`folder'/Data/Midline_Raw/SEC09H", clear
reshape wide q09_915b, i(www hh sn idc) j(q09_915a)
local foods "solid semisolid mashed liquidy milk biscuit horlicks cerelac haluwa noodles egg meat bread fruits others"
local i = 1
local n : word count `foods'
while `i' < = `n' {
local change : word `i' of `foods'
rename q09_915b`i' youngest_regfood_`change'
label variable youngest_regfood_`change' "915. `change' usually fed to the youngest child"
local i = `i' + 1
}
tempfile SEC09H_wide
save "`SEC09H_wide'", replace

use "`midline_raw_women'", clear
merge 1:1 www hh sn idc using "`SEC09H_wide'", nogen
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC09I", nogen
rename q09_916 youngest_past6months_vitaminA
rename q09_917 youngest_hoursaway
rename q09_918 youngest_expressmilk
save "`midline_raw_women'", replace

merge 1:1 www hh sn idc using "`folder'/Data/Midline_Raw/SEC10A", nogen
rename q10_1001 woman_weight
rename q10_1002 woman_height
rename q10_1003a woman_numkids
drop q10*
save "`midline_raw_women'", replace

use "`folder'/Data/cosampling.dta", clear
keep if sampco == 1
keep vdc control_vdc info_vdc cash_vdc
by vdc, sort: keep if _n == 1
rename vdc vdc_name
merge 1:m vdc_name using "`midline_raw_women'", nogen
sort www hh
/* Note: Since this is a 25% sample, will have a bunch of VDC's merged in that
   don't have a treatment status */
drop if round_num == .

/* Generate one variable with all three treatment indicators, and label it,
   for the purpose of generating tables */
gen treatment = 1 if control_vdc == 1
replace treatment = 2 if info_vdc == 1
replace treatment = 3 if cash_vdc == 1

label define treatgroup 1 "Control" 2 "Info Only" 3 "Info + Cash"
label values treatment treatgroup

/* Generate the three comparison dummies so that we can do 2-way
   hypothesis testing */
gen cash_control = cash_vdc
replace cash_control = . if info_vdc == 1

gen info_control = info_vdc
replace info_control = . if cash_vdc == 1

gen cash_info = cash_vdc
replace cash_info = . if control_vdc == 1

save "`midline_raw_women'", replace

/* Want to get the age of the youngest child, and his/her ID number */
merge 1:m www hh sn idc using "`folder'/Data/Midline_Raw/SEC10B", keepusing(q10_idc) nogen

rename q10_idc q03_idc
replace q03_idc = idc if q03_idc == .

merge 1:1 www hh sn q03_idc using "`folder'/Data/Midline_Raw/SEC03", keepusing(q03_304a q03_304b) keep(match) nogen

/* So we don't lose those without a child, replace age to missing if its the woman */
replace q03_304a = . if q03_idc == idc
replace q03_304b = . if q03_idc == idc
gen youngest_age_months = q03_304a*12+q03_304b

by www hh sn idc, sort: egen youngest_kid = min(youngest_age_months)

drop if youngest_kid ~= youngest_age_months
by www hh sn idc, sort: keep if _n == 1

drop youngest_kid q03_304a q03_304b
rename q03_idc youngest_idc

/* Add on the baseline IDC for women, from SEC00.dta */
rename idc q00_idc
merge 1:1 www hh sn q00_idc using "`folder'/Data/Midline_Raw/SEC00", nogen keep(match) keepusing(q00_001a)
rename q00_idc idc


save "`folder'/Data/midline_raw_women", replace
